package com.netwit.common.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class SpringContextUtil implements ApplicationContextAware {
    private static ApplicationContext context;

    public void setApplicationContext(ApplicationContext ctx) throws BeansException {
        SpringContextUtil.context = ctx;
    }

    public static ApplicationContext getContext() { return context; }

    // 通过名称获取 bean
    public static Object getBean(String name) {
        return getContext().getBean(name);
    }

    // 通过指定的 class 获取 bean
    public static <T> T getBean(Class<T> c) {
        return getContext().getBean(c);
    }

    //通过名称和class 获取bean
    public static <T> T getBean(String name, Class<T> c) {
        return getContext().getBean(name, c);
    }

}